package com.yimils.core.goods.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yimils.core.goods.entity.EnvironmentEntity;
import com.yimils.core.goods.entity.GoodsEnvironmentEntity;
import com.yimils.core.goods.mapper.GoodsEnvironmentMapper;
import com.yimils.core.goods.serivce.EnvironmentService;
import com.yimils.core.goods.serivce.GoodsEnvironmentService;
import com.yimils.core.goods.vo.GoodsEnvironmentRes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("goodsEnvironmentService")
public class GoodsEnvironmentServiceImpl extends ServiceImpl<GoodsEnvironmentMapper, GoodsEnvironmentEntity> implements GoodsEnvironmentService {
    @Resource(name = "environmentService")
    EnvironmentService environmentService;

    @Override
    public Boolean saveGoodsEnvironment(List<Long> environmentIds, Long goodsId) {
        List<GoodsEnvironmentEntity> collect = environmentIds.stream().map(o -> {
            GoodsEnvironmentEntity goodsEnvironmentEntity = new GoodsEnvironmentEntity();
            goodsEnvironmentEntity.setGoodsId(goodsId);
            goodsEnvironmentEntity.setEnvironmentId(o);
            goodsEnvironmentEntity.setState(Boolean.FALSE);
            goodsEnvironmentEntity.setCreateTime(LocalDateTime.now());
            goodsEnvironmentEntity.setUpdateTime(LocalDateTime.now());
            return goodsEnvironmentEntity;
        }).collect(Collectors.toList());
        return saveOrUpdateBatch(collect);
    }

    @Override
    public Boolean deleteGoodsEnvironment(List<Long> environmentIds, Long goodsId) {
        QueryWrapper<GoodsEnvironmentEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(GoodsEnvironmentEntity::getGoodsId, goodsId)
                .eq(GoodsEnvironmentEntity::getState, Boolean.FALSE);
        List<GoodsEnvironmentEntity> entities = list(qw);
        entities.forEach(o -> {
            o.setState(Boolean.TRUE);
            o.setUpdateTime(LocalDateTime.now());
        });

        return saveGoodsEnvironment(environmentIds, goodsId);
    }

    @Override
    public Map<Long, List<GoodsEnvironmentRes>> mapListGoodsEnvironment(List<Long> goodsIds) {
        QueryWrapper<GoodsEnvironmentEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(GoodsEnvironmentEntity::getGoodsId, goodsIds)
                .eq(GoodsEnvironmentEntity::getState, Boolean.FALSE);
        List<GoodsEnvironmentEntity> entities = list(qw);

        List<Long> environmentIds = entities.stream().map(GoodsEnvironmentEntity::getEnvironmentId).collect(Collectors.toList());
        List<EnvironmentEntity> pictureEntities = environmentService.listEnvironment(environmentIds);
        Map<Long, String> videoMap = pictureEntities.stream().collect(Collectors.toMap(EnvironmentEntity::getId, EnvironmentEntity::getEnvironmentName));
        return entities.stream().map(o -> {
            GoodsEnvironmentRes res = new GoodsEnvironmentRes();
            res.setGoodsId(o.getGoodsId());
            res.setEnvironmentId(o.getEnvironmentId());
            res.setEnvironmentName(videoMap.get(o.getId()));
            return res;
        }).collect(Collectors.groupingBy(GoodsEnvironmentRes::getGoodsId));
    }
}
